home *** CD-ROM | disk | FTP | other *** search
/ Greenhouse Effect Detection Expriment / NASA Greenhouse Effect Detection Expriment 1992 - Disc 2.iso / software / dos / cdf22pc / src / lib / cdfstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-09  |  4.9 KB  |  159 lines

  1. /******************************************************************************
  2. *
  3. *  NSSDC/CDF                      CDF character string handling.
  4. *
  5. *  Version 1.3, 10-Feb-92, ST Systems (STX)
  6. *
  7. *  Modification history:
  8. *
  9. *   V1.0  16-May-91, J Love    Original version (for CDF V2.1).  This is a
  10. *                combination of nulterminate.c, isdescr.c,
  11. *                descrtoref.c, and descrtorefnul.c.
  12. *   V1.1  31-Jul-91, J Love    Added 'array' class to checking for a
  13. *                descriptor (DSC$K_CLASS_A).
  14. *   V1.2  23-Sep-91, J Love    Modified for IBM-PC port.
  15. *   V1.3  10-Feb-92, J Love    CDF V2.2.
  16. *
  17. ******************************************************************************/
  18.  
  19. #include "cdflib.h"
  20.  
  21. /******************************************************************************
  22. *  NOTE:
  23. *   Never assume that a string may be NUL-terminated already.  If it is,
  24. *   it might be accidental and the NUL might go away.
  25. ******************************************************************************/
  26.  
  27. #define    NOMINAL_CDF_STRING_LENGTH    (128+1)
  28. #define    MAX_CDF_CALL_STRINGS        10    /* maximum available strings
  29.                            per CDF call */
  30.  
  31.  
  32. /******************************************************************************
  33. *  NUL-terminate a character string.
  34. ******************************************************************************/
  35.  
  36. char *NULterminate (string, length)
  37. char    *string;            /* string to NUL-terminate */
  38. size_t    length;                /* length of string */
  39. {
  40. long        string_n;    /* string to use */
  41. char        *string_ptr;    /* pointer to string being used to pass
  42.                    by reference */
  43.  
  44. static long    next_string_n = 0;    /* next string to use */
  45.  
  46. static char     strings[MAX_CDF_CALL_STRINGS][NOMINAL_CDF_STRING_LENGTH+1];
  47. static char    *aux_strings[MAX_CDF_CALL_STRINGS];
  48. static size_t    aux_lengths[MAX_CDF_CALL_STRINGS] =
  49.                     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  50.  
  51. string_n = next_string_n;
  52. next_string_n = (next_string_n + 1) % MAX_CDF_CALL_STRINGS;
  53.  
  54. if (length <= NOMINAL_CDF_STRING_LENGTH)
  55.    string_ptr = strings[string_n];
  56. else
  57.    if (length <= aux_lengths[string_n])
  58.      string_ptr = aux_strings[string_n];
  59.    else {
  60.      if (aux_strings[string_n] != NULL) free (aux_strings[string_n]);
  61.      aux_strings[string_n] = (char *) malloc (length + 1);
  62.      if (aux_strings[string_n] == NULL) return NULL;          /* Very bad. */
  63.      aux_lengths[string_n] = length;
  64.      string_ptr = aux_strings[string_n];
  65.    }
  66.  
  67. strncpy (string_ptr, string, length);    /* C will never look at anything past
  68.                        the NUL so don't copy past it like
  69.                        'memmove' would */
  70. *(string_ptr + length) = NUL;        /* still NUL terminate in case 'string'
  71.                        didn't contain a NUL before 'length'
  72.                        was reached */
  73.  
  74. return string_ptr;
  75. }
  76.  
  77.  
  78. #if defined(vms)
  79. /******************************************************************************
  80. *  Is a descriptor being pointed to?
  81. *
  82. *  NOTE:
  83. *    There is no checking on the length of the string because all lengths
  84. *  are valid to VMS and to CDF (for attributes with character data types).
  85. ******************************************************************************/
  86.  
  87. #include <descrip.h>
  88.  
  89. Boolean isDESCR (ptr, aptr, len)
  90. void    *ptr;        /* could point to a descriptor or to the
  91.                string (if already passed by reference) */
  92. char    **aptr;        /* pointer to string */
  93. size_t    *len;        /* length of string */
  94. {
  95. struct dsc$descriptor *Tptr = (struct dsc$descriptor *) ptr;
  96.  
  97. if (Tptr->dsc$b_dtype == DSC$K_DTYPE_T &&
  98.     (Tptr->dsc$b_class == DSC$K_CLASS_S ||
  99.      Tptr->dsc$b_class == DSC$K_CLASS_A)) {
  100.   *len = Tptr->dsc$w_length;
  101.   *aptr = Tptr->dsc$a_pointer;
  102.   return TRUE;
  103. }
  104. else
  105.   return FALSE;
  106.  
  107. }
  108. #endif
  109.  
  110.  
  111. #if defined(vms)
  112. /******************************************************************************
  113. *  Convert from passing by descriptor to passing by reference.
  114. ******************************************************************************/
  115.  
  116. char *DESCRtoREF (ptr)
  117. void    *ptr;        /* could point to a descriptor or to the string (if
  118.                already passed by reference) */
  119. {
  120. char    *aptr;        /* pointer to string */
  121. size_t    len;        /* length of string */
  122.  
  123. if (isDESCR (ptr, &aptr, &len))
  124.   return aptr;
  125. else
  126.   return (char *) ptr;             /* already passed by reference */
  127.  
  128. }
  129. #endif
  130.  
  131.  
  132. #if defined(vms)
  133. /******************************************************************************
  134. *  Convert from passing by descriptor to passing by reference and
  135. *  NUL-terminate.
  136. *
  137. *  NOTE:
  138. *   If a descriptor is being pointed to, always use a temporary string to pass
  139. *   by reference - never assume that the string may already be NUL-terminated
  140. *   (in case its NUL-terminated by chance and the NUL might go away).
  141. *
  142. ******************************************************************************/
  143.  
  144. char *DESCRtoREFnul (ptr, maxREFlen)
  145. void    *ptr;        /* could point to a descriptor or to the string (if
  146.                already passed by reference) */
  147. size_t    maxREFlen;    /* maximum length of the string if passed by
  148.                reference */
  149. {
  150. char    *aptr;        /* pointer to string */
  151. size_t    len;        /* length of string */
  152.  
  153. if (isDESCR (ptr, &aptr, &len))
  154.   return NULterminate (aptr, len);
  155. else
  156.   return NULterminate (ptr, maxREFlen);
  157. }
  158. #endif
  159.